/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.classes.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.classes.bo.UserBindingBo;
import org.springblade.modules.classes.excel.ClassUserImporter;
import org.springblade.modules.classes.service.IUserBindingService;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.classes.entity.ClassesUserEntity;
import org.springblade.modules.classes.vo.ClassesUserVO;
import org.springblade.modules.classes.excel.ClassesUserExcel;
import org.springblade.modules.classes.wrapper.ClassesUserWrapper;
import org.springblade.modules.classes.service.IClassesUserService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

/**
 * 班级学员表 控制器
 *
 * @author junxin
 * @since 2023-04-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-classesUser/classesUser")
@Api(value = "班级学员表", tags = "班级学员表接口")
public class ClassesUserController extends BladeController {

	private final IClassesUserService classesUserService;
	private final IUserBaseService userBaseService;
	private final IUserBindingService userBindingService;
	/**
	 * 班级学员表 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入classesUser")
	public R<ClassesUserVO> detail(ClassesUserEntity classesUser) {
		ClassesUserEntity detail = classesUserService.getOne(Condition.getQueryWrapper(classesUser));
		return R.data(ClassesUserWrapper.build().entityVO(detail));
	}

	/**
	 * 班级学员表 自定义分页
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入classesUser")
	public R<IPage<ClassesUserVO>> page(ClassesUserVO classesUser, Query query) {
		IPage<ClassesUserVO> pages = classesUserService.selectClassesUserPage(Condition.getPage(query), classesUser);
		return R.data(pages);
	}

	/**
	 * 班级学员表 新增
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入classesUser")
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@ServiceLog(value = TrainingOperateConstant.CLASSES, operate = TrainingOperateConstant.ADD, describe = "班级学员新增")
	public R save(@Valid @RequestBody UserBindingBo bo) {
		userBindingService.addUserBinding(bo);
		return R.success("保存成功");
	}

	/**
	 * 班级学员表 修改
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入classesUser")
	@ServiceLog(value = TrainingOperateConstant.CLASSES, operate = TrainingOperateConstant.UPDATE, describe = "班级学员修改")
	public R update(@Valid @RequestBody ClassesUserEntity classesUser) {
		if (null == classesUser.getUserId()) throw new ServiceException("请选择学员");
		if (null == classesUser.getClassesId()) throw new ServiceException("请选择班级");
		return R.status(classesUserService.updateById(classesUser));
	}

	/**
	 * 班级学员表 删除
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@ServiceLog(value = TrainingOperateConstant.CLASSES, operate = TrainingOperateConstant.DELETE, describe = "班级学员删除")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> longList = Func.toLongList(ids);
		return R.status(classesUserService.deleteLogic(longList));
	}


	/**
	 * 导出数据
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/export-classesUser")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入classesUser")
	public void exportClassesUser(@ApiIgnore @RequestParam Map<String, Object> classesUser, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<ClassesUserEntity> queryWrapper = Condition.getQueryWrapper(classesUser, ClassesUserEntity.class);
		queryWrapper.lambda().eq(ClassesUserEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<ClassesUserExcel> list = classesUserService.exportClassesUser(queryWrapper);
		ExcelUtil.export(response, "班级学员表数据" + DateUtil.time(), "班级学员表数据表", list, ClassesUserExcel.class);
	}


	/**
	 * 批量导入班级学员
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("import-user")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导入班级学员", notes = "传入classesId")
	public R importQuestionBank(MultipartFile file, Long classesId) {
		if (null == classesId) throw new ServiceException("请先选择班级");
		ClassUserImporter classUserImporter = new ClassUserImporter(classesUserService, userBaseService, classesId);
		ExcelUtil.save(file, classUserImporter, ClassesUserExcel.class);
		return R.success("操作成功");
	}

	/**
	 * 导出模板
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("export-template")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<ClassesUserExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "班级学员数据模板", "班级学员数据表", list, ClassesUserExcel.class);
	}


}
